package com.ja.openchat.ui.me;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentRecordBinding;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;

public abstract class AbsRecordFragment<T> extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, OnLoadMoreListener {


    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_record;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        ((FragmentRecordBinding) binding).recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = newIntanceAdapter();
        ((FragmentRecordBinding) binding).recyclerView.setAdapter(mAdapter);
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(this);
        ((FragmentRecordBinding) binding).swipeRefreshLayout.setOnRefreshListener(this);
        ((FragmentRecordBinding) binding).swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                ((FragmentRecordBinding) binding).swipeRefreshLayout.setRefreshing(true);
                requestData(currentPage);
            }
        });
    }

    @Override
    public BaseViewModel initViewModel() {
        return new BaseViewModel(getActivity().getApplication());
    }

    protected abstract BaseQuickAdapter<T, BaseViewHolder> newIntanceAdapter();

    protected BaseQuickAdapter<T, BaseViewHolder> mAdapter;

    @Override
    public void onRefresh() {
        currentPage = 1;
        requestData(currentPage);
    }

    @Override
    public void onLoadMore() {
        currentPage++;
        requestData(currentPage);
    }

    protected abstract void requestData(int page);

    protected final int PAGE_COUNT = 20;
    protected int currentPage = 1;

    protected void finishRefreshAndLoad(int page, List<T> objects) {
        if (page == 1) {
            ((FragmentRecordBinding) binding).swipeRefreshLayout.setRefreshing(false);
            mAdapter.setNewInstance(objects);
        } else {
            if (objects != null && objects.size() > 0) {
                mAdapter.addData(objects);
            }
        }
        if (objects == null || objects.size() < PAGE_COUNT) {
            mAdapter.getLoadMoreModule().loadMoreEnd(true);
        } else {
            mAdapter.getLoadMoreModule().loadMoreComplete();
        }
    }
}
